package com.zds.hj.poseidon.biz.cba.hpp.domain.credit.service.nativ;

import com.zds.boot.common.domain.DomainService;
import com.zds.boot.common.exception.BusinessException;
import com.zds.boot.common.facade.page.Sort;
import com.zds.boot.common.utils.mapper.BeanCopier;
import com.zds.hj.poseidon.biz.cba.hpp.domain.credit.dao.CreditInfoEntityDao;
import com.zds.hj.poseidon.biz.cba.hpp.domain.credit.entity.CreditInfoEntity;
import com.zds.hj.poseidon.facade.dto.hpp.credit.CreditInfoEntityDto;
import com.zds.zly.common.enums.HjResultCodeEnum;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author duanhao
 * @Description 授信DomainService
 * @Date 2023/5/30 14:48
 */
@DomainService
@Slf4j
public class CreditApplyDomainService {
    @Resource
    private CreditInfoEntityDao creditInfoEntityDao;

    /**
     * 条件查询
     *
     * @param map
     * @param sort
     * @return
     */
    public List<CreditInfoEntity> findList(Map<String, Object> map, Sort sort) {
        return creditInfoEntityDao.findAll(map, sort);
    }

    /**
     * 保存授信信息
     *
     * @param creditInfoEntityDto
     * @return
     */
    public CreditInfoEntityDto insert(CreditInfoEntityDto creditInfoEntityDto) {
        CreditInfoEntity entity = new CreditInfoEntity();
        entity.from(creditInfoEntityDto);
        creditInfoEntityDao.create(entity);
        BeanCopier.copy(entity, creditInfoEntityDto);
        return creditInfoEntityDto;
    }

    /**
     * 修改授信信息
     *
     * @param creditInfoEntityDto
     * @return
     */
    public CreditInfoEntityDto update(CreditInfoEntityDto creditInfoEntityDto) {
        CreditInfoEntity entity = creditInfoEntityDao.get(creditInfoEntityDto.getId());
        if (null == entity) {
            throw new BusinessException("未查询到该授信申请", HjResultCodeEnum.NO_DATA_FOUND.code());
        }
        entity.from(creditInfoEntityDto);
        creditInfoEntityDao.update(entity);
        BeanCopier.copy(entity, creditInfoEntityDto);
        return creditInfoEntityDto;
    }


}
